import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/register.vue'
import Change from '../views/change.vue'
import Admin from '../views/admin.vue'
import List from '../views/list.vue'
import Announcement from '../views/announcement.vue'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// VueRouter.beforeEach((to, from, next) => {
//   // ...
//   if (sessionStorage.getItem('token')) {
//     next()
//   } else {
//     this.$route.push('/login')
//   }
// })





const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/change',
    name: 'Change',
    component: Change
  },
  {
    path: '/home',
    name: '商品首页',
    component: Home,
    meta: { isLog: true }
    // children: [
    //   {
    //     path: '/home/home',
    //     // component: () => import('../views/home/home.vue')
    //     redirect: '/home'
    //   },
    //   {
    //     path: '/home/list',
    //     component: () => import('../views/home/list.vue')
    //   }
    // ]
  },
  {
    path: '/list',
    name: '商品列表',
    component: List,
    children: [
      {
        path: '/list',
        name: '商品列表',
        component: () => import('../views/list/all.vue')
      },
      {
        name: '商品列表',
        path: '/digital',
        component: () => import('../views/list/3c.vue')
      },
      {
        name: '商品列表',
        path: '/books',
        component: () => import('../views/list/books.vue')
      },
      {
        name: '商品列表',
        path: '/life',
        component: () => import('../views/list/life.vue')
      },
      {
        name: '商品列表',
        path: '/sandc',
        component: () => import('../views/list/sandc.vue')
      },
      {
        name: '商品列表',
        path: '/beauty',
        component: () => import('../views/list/beauty.vue')
      },
      {
        name: '商品列表',
        path: '/others',
        component: () => import('../views/list/others.vue')
      },
      {
        name: '商品列表',
        path: '/search/:value',
        component: () => import('../views/list/search.vue')
      }
    ]
  },
  {
    path: '/admin',
    name: 'Admin',
    component: Admin,
    meta: { isLog: true, type: 'super' },
    children: [
      {
        path: '/admin/user',
        component: () => import('../views/admin/user.vue')
      },
      {
        path: '/admin/adminuser',
        component: () => import('../views/admin/adminuser.vue')
      },
      {
        path: '/admin/edituser/:id',
        component: () => import('../views/admin/edituser.vue'),
        props: true
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/goodsdetail/:col/:id',
    name: '商品详情',
    props: true,
    component: () => import('../views/list/detail.vue')
  },
  {
    path: '/sellsdetail/:col/:id',
    name: '订单详情',
    props: true,
    component: () => import('../views/user/sellsdetail.vue')
  },
  {
    path: '/userdetail',
    name: '个人中心',
    component: () => import('../views/userdetail.vue'),
    children: [
      {
        path: '/shoucang',
        component: () => import('../views/user/shoucang.vue')
      },
      {
        path: '/mybuy',
        component: () => import('../views/user/mybuy.vue')
      },
      {
        path: '/mysell',
        component: () => import('../views/user/mysell.vue')
      },
      {
        path: '/upbaby',
        component: () => import('../views/user/upbaby.vue')
      }
    ]
  },
  {
    path: '/announcement',
    name: '公告',
    component: Announcement
  }
]

const router = new VueRouter({
  routes
})



router.beforeEach((to, from, next) => {
  // ...
  if (to.meta.isLog) {
    next()
    if (sessionStorage.getItem('token')) {
      next()
    } else {
      alert('请登录')
      next(from.path)
    }
  } else {
    next()
  }
  if (to.meta.type === 'super') {
    if (sessionStorage.getItem('type') === '超级管理员') {
      next()
    } else {
      alert('无权限访问')
      next(from.path)
    }
  }
})



export default router
